﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using System.Globalization;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class WF_DetailLoanPayment : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        Main main = new Main();
        private object _dataItem = null;        

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                BindComboYear();
                lblPeriod.Text = Session["amt_period"].ToString();             

                this.ViewState.Add("IsUserControlPostBack", true);

            }

            if (DataItem != null)
            {

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                   
                    txtPayDate.SetValue(DataBinder.Eval(DataItem, "pay_date", "{0:dd/MM/yyyy}")); 
                    lblContDtlseq.Text = DataBinder.Eval(DataItem, "cont_dtls_seq").ToString();
                    txtPeriod.Text = DataBinder.Eval(DataItem, "period").ToString();                  
                    cmbMonth.SelectedValue = DataBinder.Eval(DataItem, "fiscal_month").ToString();
                    cmbYear.SelectedValue = DataBinder.Eval(DataItem, "fiscal_year").ToString();
                    txtAmount.Text = DataBinder.Eval(DataItem, "pay_amount").ToString();
                    txtInterest.Text = DataBinder.Eval(DataItem, "pay_interest").ToString();
                    rdoPayFlag.SelectedValue = DataBinder.Eval(DataItem, "pay_flag").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();
                }

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {



        }

        private void BindComboYear()
        {
            int curYear = int.Parse(DateTime.Today.Year.ToString());
            curYear += 543;
            int MinYear = curYear - 10;
            int MaxYear = curYear + 10; 
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = MinYear; i <= MaxYear; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }

            cmbYear.DataSource = dt;
            cmbYear.DataBind();
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                WelfareLoanPaymentBLL obj = new WelfareLoanPaymentBLL();


                int cont_seq = int.Parse(Session["cont_seq"].ToString());   

                string pay_date, remarks;
                int period, fiscal_month, fiscal_year, pay_flag;
                double pay_amount, pay_interest;

                pay_date = txtPayDate.GetStringValue(); 
                remarks = txtRemarks.Text;
                period = int.Parse(txtPeriod.Text);
                fiscal_month = int.Parse(cmbMonth.SelectedValue );
                fiscal_year = int.Parse(cmbYear.SelectedValue );
                pay_flag = int.Parse(rdoPayFlag.SelectedValue );
                pay_amount = int.Parse(txtAmount.Text);
                pay_interest = int.Parse(txtInterest.Text);

                //if ((int.Parse(txtPeriod.Text) > int.Parse(lblPeriod.Text)) && (!obj.chkDupPeriod(period)))

                if ((int.Parse(txtPeriod.Text) > int.Parse(lblPeriod.Text)))
                {
                    lblWarningDup.Visible = true;
                    lblWarningDup.Text = "งวดที่ซ้ำ";
                    return;
                }
                else
                {
                    lblWarningDup.Visible = false;

                }
            


                if (obj.chkDupPeriod(cont_seq, period) == true)
                {
                    main.MessageAlert(this.Page, "งวดที่ซ้ำ");
                    //lblWarningDup.Visible = true;
                    //lblWarningDup.Text = "งวดที่ซ้ำ";
                    return;
                }
                else
                {
                    lblWarningDup.Visible = false;

                }
            

                obj.insert(cont_seq,pay_date, remarks, period, fiscal_month, fiscal_year, 
                    pay_flag, pay_amount, pay_interest, ssMng.EmpSeqI);


            }
            catch (Exception ex)
            {



            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            WelfareLoanPaymentBLL obj = new WelfareLoanPaymentBLL();

            //int cont_seq = int.Parse(Session["cont_seq"].ToString());

            string pay_date, remarks;
            int cont_dtls_seq,period, fiscal_month, fiscal_year, pay_flag;
            double pay_amount, pay_interest;

            cont_dtls_seq = int.Parse(lblContDtlseq.Text);
            pay_date = txtPayDate.GetStringValue();
            remarks = txtRemarks.Text;
            period = int.Parse(txtPeriod.Text);
            fiscal_month = int.Parse(cmbMonth.SelectedValue);
            fiscal_year = int.Parse(cmbYear.SelectedValue);
            pay_flag = int.Parse(rdoPayFlag.SelectedValue);
            pay_amount = int.Parse(txtAmount.Text);
            pay_interest = int.Parse(txtInterest.Text);

            if ((int.Parse(txtPeriod.Text) > int.Parse(lblPeriod.Text)))
            {
                return;
            }
            if (obj.chkDupPeriodEdit(period, cont_dtls_seq) == true)
            {
                lblWarningDup.Visible = true;
                lblWarningDup.Text = "งวดที่ซ้ำ";
                return;
            }
            else
            {
                lblWarningDup.Visible = false;
                obj.Update(cont_dtls_seq, pay_date, remarks, period, fiscal_month, fiscal_year, pay_flag, pay_amount,
                pay_interest, ssMng.EmpSeqI);

            }        


        }      

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

    }
}
